#include <bits/stdc++.h>

using namespace std;

class Spreadsheet {
    vector<vector<int>> sheet;
public:
    Spreadsheet(int rows) {
        sheet.resize(rows);
        for(int i = 0;i < rows;i++){
            sheet[i].resize(26,0);
        }
    }
    
    void setCell(string cell, int value) {
        int col = cell[0] - 'A',row = stoi(cell.substr(1)) - 1;
        sheet[row][col] = value;
    }
    
    void resetCell(string cell) {
        int col = cell[0] - 'A',row = stoi(cell.substr(1)) - 1;
        sheet[row][col] = 0;
    }
    
    int getValue(string formula) {
        int pos = formula.find('+');
        string s1 = formula.substr(1,pos - 1);
        string s2 = formula.substr(pos + 1);
        return stringval(s1) + stringval(s2);
    }
    int stringval(string& str){
        if(isalpha(str[0])){
            int col = str[0] - 'A',row = stoi(str.substr(1)) - 1;
            return sheet[row][col];
        }
        return stoi(str);
    }
};

class TaskManager {
private:
    unordered_map<int, pair<int, int>> taskInfo;
    priority_queue<pair<int, int>> heap;
public:
    TaskManager(vector<vector<int>> tasks) {
        for (auto& task : tasks) {
            int userId = task[0], taskId = task[1], priority = task[2];
            taskInfo[taskId] = {priority, userId};
            heap.push({priority, taskId});
        }
    }
    
    void add(int userId, int taskId, int priority) {
        taskInfo[taskId] = {priority, userId};
        heap.push({priority, taskId});
    }
    
    void edit(int taskId, int newPriority) {
        if (taskInfo.find(taskId) != taskInfo.end()) {
            taskInfo[taskId].first = newPriority;
            heap.push({newPriority, taskId});
        }
    }
    
    void rmv(int taskId) {
        taskInfo.erase(taskId);
    }
    
    int execTop() {
        while (!heap.empty()) {
            auto& [priority, taskId] = heap.top();
            if (taskInfo.find(taskId) != taskInfo.end() && taskInfo[taskId].first == priority) {
                int userId = taskInfo[taskId].second;
                taskInfo.erase(taskId);
                heap.pop();
                return userId;
            }
            heap.pop();
        }
        return -1;
    }
};
